package com.fr71.project.dm.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class QnUploadUtil {
    private static final Logger logger = LoggerFactory.getLogger(QnUploadUtil.class);

    public static final String accessKeyAliyun = "LTAI5tQaVfigN1dw7Pb7fPDr";
    public static final String secretKeyAliyun = "G5WyP4MkeSEBTWaBfUeUPp5v11v4u3";
    public static final String bucketAliyun = "20250609";

    public static String uploadAliyun(String fileKey, File file) {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyAliyun, secretKeyAliyun);

        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketAliyun, fileKey, file);

            // 上传文件。
            ossClient.putObject(putObjectRequest);

            String res = "https://20250609.oss-cn-beijing.aliyuncs.com/" + fileKey;
            logger.info("阿里云网址路径为:{}", res);
            return res;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
            return null;
        } catch (Exception ce) {
            System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}